# -*- coding: utf-8 -*-
# @Time    : 2021/5/27 12:53
# @Author  : Leith14
# @Email   : leixuewen14@126.com
from typing import List

import pytest

from Pytest_homework.Calculator import Calculator


@pytest.fixture(scope='module')
def a():
    print('test auto')


@pytest.fixture()
def login():
    print('loging')
    username = 'hogwarts'
    password = '123'
    return username, password


@pytest.fixture(scope='session')
def connect_db():
    print('连接数据库')
    # 用yield关键字代替return可执行teardown语句
    yield '搜索结果'
    print('断开数据库连接')  # 等于teardown


@pytest.fixture(scope='class')
def initcalc_class():
    # setup
    print("开始计算")
    calc = Calculator()
    yield calc  #下次在执行这个方法就从yield之后开始执行了
    # return calc
    # teardown
    print("结束计算")


@pytest.fixture(params=[[0.1, 0, False], [2, 2, 1]], ids=['zero', 'int1'])
def get_div_datas(request):
    return request.param


def pytest_collection_modifyitems(session, config, items: List):
    print('这是收集所有测试用例的方法')
    print(items)
    items.reverse()
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')
        if 'foo' in item.name:
            item.add_marker(pytest.mark.foo)
        elif 'last' in item.name:
            item.add_marker(pytest.mark.last)